package com.promis.pfs;

import android.content.Context;

import com.promis.okhttp.HttpFileParams;
import com.promis.okhttp.HttpUploadClient;
import com.promis.util.CollectionUtils;
import com.promis.util.FileUtils;
import com.promis.util.StringUtils;

import java.io.File;

public abstract class PfsFileStorageClient {

    private static String IMAGE_URL;
    private static String VIDEO_URL;

    protected Context context;

    public static void init(String imageStorageUrl, String videoStorageUrl) {
        IMAGE_URL = imageStorageUrl;
        VIDEO_URL = videoStorageUrl;
    }

    public PfsFileStorageClient(Context context) {
        this.context = context;
    }

    public void uploadImages( String... localPaths) {
        upload(IMAGE_URL, localPaths);
    }

    public void uploadImages(File... localFiles) {
        upload(IMAGE_URL, localFiles);
    }

    public void uploadVideos(String... localPaths) {
        upload(VIDEO_URL, localPaths);
    }

    public void uploadVideos(File... localFiles) {
        upload(VIDEO_URL, localFiles);
    }

    private void upload(String url, String... localPaths) {
        if (context == null || StringUtils.isNullOrEmpty(url) || CollectionUtils.isNullOrEmpty(localPaths)) return;

        HttpFileParams httpFileParams = new HttpFileParams();
        for (String localPath : localPaths) {
            File localFile = new File(localPath);
            if (StringUtils.isNullOrEmpty(localPath) || !localFile.exists()) {
                PfsFileStorageClient.this.onError("上传文件路径不存在");
                return;
            }
            byte[] bytes = FileUtils.readFileToBytes(localFile);
            httpFileParams.add("imgFile", "", bytes);
        }

        upload(url, httpFileParams, localPaths.length);
    }

    private void upload(String url, File... localFiles) {
        if (context == null || StringUtils.isNullOrEmpty(url) || CollectionUtils.isNullOrEmpty(localFiles)) return;

        HttpFileParams httpFileParams = new HttpFileParams();
        for (File localFile : localFiles) {
            if (!localFile.exists()) {
                PfsFileStorageClient.this.onError("上传文件路径不存在");
                return;
            }
            byte[] bytes = FileUtils.readFileToBytes(localFile);
            httpFileParams.add("imgFile", "", bytes);
        }

        upload(url, httpFileParams, localFiles.length);
    }

    private void upload(String url, HttpFileParams httpFileParams, final int count) {
        onReady();
        new HttpUploadClient<PfsJsonFiles>(context) {

            @Override
            protected void onSuccess(PfsJsonFiles result) {
                if (count != result.getPaths().length) {
                    PfsFileStorageClient.this.onError("返回服务器存储路径数量与上传文件数量不一致");
                    return;
                }
                PfsFileStorageClient.this.onSuccess(result.getPaths());
            }

            @Override
            protected void onError(String errorMsg) {
                PfsFileStorageClient.this.onError(errorMsg);
            }

            @Override
            protected void onFinish() {
                PfsFileStorageClient.this.onFinish();
            }

            @Override
            public void onRequestProgress(long bytesWritten, long contentLength, boolean done) {
                PfsFileStorageClient.this.onProgress(bytesWritten, contentLength, done);
            }
        }.postAsyn(url, httpFileParams);
    }

    protected abstract void onReady();

    protected abstract void onSuccess(String... paths);

    protected void onError(String errorMsg) {}

    protected void onFinish(){}

    public void onProgress(long bytesWritten, long contentLength, boolean done) {};
}
